package com.tansun.easycare.rule.common.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tansun.easycare.rule.common.model.RuleSetParamInfo;
import com.tansun.easycare.rule.common.service.RuleSetParmOperator;
import com.tansun.easycare.rule.decisiontree.service.DecisionTreeService;
import com.tansun.easycare.rule.referencedata.entity.RuleReferenceData;
import com.tansun.easycare.rule.referencedata.service.RuleReferenceDataService;
import com.tansun.easycare.rule.ruleflow.service.RuleFlowService;
import com.tansun.easycare.rule.rulegroup.service.RuleGroupService;
import com.tansun.easycare.rule.scoremodel.service.ScoreModelService;
import com.tansun.easycare.rule.table.singletblindex.service.SingleTblIndexService;
import com.tansun.rule.common.RuleModelTypeEnum;

@Service
public class RuleSetParamHandler implements RuleSetParmOperator {
	@Autowired
	private RuleReferenceDataService  ruleReferenceDataService;
	
	@Autowired
	private  DecisionTreeService  decisionTreeService;
	
	@Autowired 
	private  RuleGroupService  ruleGroupService;
	
	@Autowired
	private  ScoreModelService  scoreModelService;
	
	@Autowired
	private   SingleTblIndexService  singleTbleIdexService;
	
	@Autowired
	private   RuleFlowService   ruleFlowService;
	@Override
	public void upateRuleModel(String projectCode, RuleSetParamInfo newSetParamInfo, RuleSetParamInfo oldParamInfo,
			boolean setFlag)  throws Exception{
		 RuleReferenceData  refData  = new  RuleReferenceData();
		 refData.setProjectCode(projectCode);
		 if (setFlag) {
			 refData.setClassName(oldParamInfo.getCode());
		 }else {
			 refData.setNameen(oldParamInfo.getCode());
		 }
		 List<RuleReferenceData> list =this.ruleReferenceDataService.findReferenceModelList(refData);
		 Map<String,RuleReferenceData> map  = new HashMap<>();
		 for (RuleReferenceData referenceData: list) {
			 map.put(referenceData.getManageId(), referenceData);
			 if (setFlag) {
				 referenceData.setClassName(newSetParamInfo.getCode());
			 }else {
				 referenceData.setNameen(newSetParamInfo.getCode());
				 referenceData.setNamecn(newSetParamInfo.getName());
			 }
			 this.ruleReferenceDataService.save(referenceData);
		 }
		
		 for ( String k:map.keySet()) {
			    RuleReferenceData v = map.get(k);
				String  modelType = v.getModelType();
				if (RuleModelTypeEnum.DecisionTree.code.equals(modelType)) {
					decisionTreeService.updateSetParam(k, newSetParamInfo,oldParamInfo, setFlag);
				}else if (RuleModelTypeEnum.SingleTblIndex.code.equals(modelType)) {
					singleTbleIdexService.updateSetParam(k, newSetParamInfo,oldParamInfo, setFlag);
				}else if (RuleModelTypeEnum.ScoreModel.code.equals(modelType)) {
					scoreModelService.updateSetParam(k, newSetParamInfo,oldParamInfo, setFlag);
				}else if (RuleModelTypeEnum.RuleFlow.code.equals(modelType)) {
					ruleFlowService.updateSetParam(k, newSetParamInfo,oldParamInfo, setFlag);
				}else if (RuleModelTypeEnum.GeneralRule.code.equals(modelType)) {
					ruleGroupService.updateSetParam(k, newSetParamInfo,oldParamInfo, setFlag);	
				}	
		 }
		
	}
	
	
	

}
